export const time = {
	//格式化时间
	formatTime(time) {
		time = this.dateFormat("YY-mm-dd HH:MM:SS", time);
		// console.log('time', time)
		var date = time.toString();
		var year = date.split("-")[0];
		var month = date.split("-")[1];
		var day = date.split("-")[2];
		var d1 = new Date(year + '/' + month + '/' + day.split(" ")[0]);
		var d3 = new Date(date.replace(/-/g, "/"));
		var dd = new Date();
		var y = dd.getFullYear();
		var m = dd.getMonth() + 1;
		var d = dd.getDate();
		var d2 = new Date(y + '/' + m + '/' + d);
		var iday = parseInt(d2 - d1) / 1000 / 60 / 60 / 24;
		var hours = d3.getHours();
		var minutes = d3.getMinutes();
		if (minutes < 10) {
			minutes = '0' + minutes;
		}
		if (hours < 10) {
			hours = '0' + hours;
		}
		if (iday == 0) {
			if (hours >= 12) {
				return "下午 " + hours + ":" + minutes;
			} else {
				return "上午 " + hours + ":" + minutes;;
			}
		} else if (iday == 1) {
			var dt = "";
			if (hours >= 12) {
				dt = "下午 " + hours + ":" + minutes;
			} else {
				dt = "上午 " + hours + ":" + minutes;;
			}
			return "昨天 " + dt;
		} else if (iday == 2) {
			var dt = "";
			if (hours >= 12) {
				dt = "下午 " + hours + ":" + minutes;
			} else {
				dt = "上午 " + hours + ":" + minutes;;
			}
			return "前天 " + dt;
		} else {
			return year + '/' + month + "/" + d1.getDate()
		}
	},
	dateFormat(fmt, time) {
		// console.log('time', time)
		if(!time){
			return '未知'
		}
		var date = new Date(time)
		let ret;
		const opt = {
			"Y+": date.getFullYear().toString(), // 年
			"M+": (date.getMonth() + 1).toString(), // 月
			"D+": date.getDate().toString(), // 日
			"h+": date.getHours().toString(), // 时
			"m+": date.getMinutes().toString(), // 分
			"s+": date.getSeconds().toString() // 秒
		};
		for (let k in opt) {
			ret = new RegExp("(" + k + ")").exec(fmt);
			if (ret) {
				fmt = fmt.replace(ret[1], (ret[1].length == 1) ? (opt[k]) : (opt[k].padStart(ret[1].length, "0")))
			};
		};
		return fmt;
	},
	//时间戳转日期 -
	timestampToDate_ymd: (timestamp)=>{
		let date = new Date(timestamp);//时间戳为10位需*1000，时间戳为13位的话不需乘1000
		let y = date.getFullYear();
		let m = date.getMonth()+1 < 10 ? '0'+(date.getMonth()+1) : date.getMonth()+1
		let d = date.getDate() < 10 ? '0'+date.getDate() : date.getDate()
		return y+'-'+m+'-'+d
	},
	//时间戳转日时分秒
	timestampToYMDHMS: (timestamp)=>{
		let date = new Date(timestamp);//时间戳为10位需*1000，时间戳为13位的话不需乘1000
		console.log('date', date)
		let y = date.getFullYear();
		let m = date.getMonth()+1 < 10 ? '0'+(date.getMonth()+1) : date.getMonth()+1
		let d = date.getDate() < 10 ? '0'+date.getDate() : date.getDate()
		let h = date.getHours() < 10 ? '0'+date.getHours() : date.getHours()
		let mm = date.getMinutes() < 10 ? '0'+ date.getMinutes() : date.getMinutes()
		let s = date.getSeconds() < 10 ? '0'+ date.getSeconds() : date.getSeconds()
		return y+'-'+m+'-'+d+' '+h+':'+mm+':'+s
	},
	dateToTimestamp: (date)=>{
		console.log('date', date)
		if(!date){
			return 0
		}
		if(date.length>=19){
			date = date.substring(0, 19).replace('T', ' ').replace(/-/g, '/');
		}
		var date = new Date(date)
		return date.getTime()
	},
	//获取当前时间的时间戳
	getNowTimestamp(){
		let date = new Date()
		return date.getTime()
	},
	//获取n天之后的时间
	getDataAfterN(date, n){
		var currentDate = date; // 获取当前日期
		currentDate.setDate(currentDate.getDate() + n); // 将日期加上15天
		return currentDate
	},
	// *****计算日期间隔（需要传入Date对象）*****
	// 计算两个日期相差的毫秒数
	calDiff_ms(startTime, endTime){
		return endTime - startTime;
	},
	// 计算两个日期相差的秒数
	calDiff_s(startTime, endTime){
		return Math.floor((endTime - startTime) / 1000);
	},
	// 计算两个日期相差的分钟数
	calDiff_min(startTime, endTime){
		return Math.floor((endTime - startTime) / 1000 / 60);
	},
	// 计算两个日期相差的小时数
	calDiff_hours(startTime, endTime){
		return Math.floor((endTime - startTime) / 1000 / 60 / 60);
	},
	// 计算两个日期相差的天数
	calDiff_day(startTime, endTime){
		return Math.floor((endTime - startTime) / 1000 / 60 / 60 / 24);
	},
}

